let requestUrl;

function addHandler() {
	let roleId = $("#role_id").combobox("getValue");

	if (roleId) {
		$("#roleId").combobox("setValue", roleId);
	}

	requestUrl = "/role_shizhuang_image/insert";

	$("#image_dialog").dialog("open");
}

function editHandler() {
	let rowData = $("#image_list").datagrid("getSelected");

	if (rowData) {
		requestUrl = "/role_shizhuang_image/updateById";

		$("#id").val(rowData.id);
		$("#roleId").combobox("setValue", rowData.roleId);
		$("#shizhuangId").combobox("setValue", rowData.shizhuangId);

		$("#image_dialog").dialog("open");
	} else {
		alertMsg("请选择要修改的记录");
	}
}

function imageHandler() {
	let rowData = $("#image_list").datagrid("getSelected");

	if (rowData) {
		$("#image_id").val(rowData.id);
		$("#img").attr("src", rowData.image);

		$("#upload_dialog").dialog("open");
	} else {
		alertMsg("请选择要修改的记录。", "warning");
	}
}

function deleteHandler(id) {
	$.messager.confirm("提示", "是否确认删除？", function(bool) {
		if (bool) {
			ajaxPost("/role_shizhuang_image/deleteById", {
				id: id
			}, function(resp) {
				showMsg(resp.message);

				$("#image_list").datagrid("reload");
			}, error);
		}
	});
}

$(document).ready(function() {

	$("#role_id").combobox({
		url: "/role_category/selectAll",
		valueField: "id",
		textField: "name",
		width: 150,
		editable: true,
		panelHeight: "auto",
		prompt: "--选择角色造型--",
		formatter: function(row) {
			return "<img width='20' src='" + row.image + "' />&nbsp;" + row.name;
		}
	});

	$("#shizhuang_id").combobox({
		url: "/shizhuang_category/selectParent",
		valueField: "id",
		textField: "name",
		width: 150,
		editable: true,
		panelHeight: "auto",
		prompt: "--选择时装类别--"
	});

	$("#search").linkbutton({
		iconCls: "icon-search"
	}).click(function() {
		let shizhuangId = $("#shizhuang_id").textbox("getValue");
		let roleId = $("#role_id").textbox("getValue");

		$("#image_list").datagrid("reload", {
			shizhuangId: shizhuangId,
			roleId: roleId
		});
	});

	$("#clear").linkbutton({
		iconCls: "icon-delete"
	}).click(function() {
		$("#search_form").form("clear");
	});

	/******************************************************************************************************/

	$("#roleId").combobox({
		url: "/role_category/selectAll",
		valueField: "id",
		textField: "name",
		width: 100,
		editable: true,
		required: true,
		panelHeight: "auto",
		formatter: function(row) {
			return "<img width='20' src='" + row.image + "' />&nbsp;" + row.name;
		},
		onSelect: function(record) {
			$("#shizhuangId").combobox("reload", {
				gender: record.gender
			});
		}
	});

	$("#shizhuangId").combobox({
		url: "/shizhuang_category/selectList",
		valueField: "id",
		textField: "name",
		width: 100,
		editable: true,
		required: true,
		panelHeight: "auto"
	});

	$("#image_dialog").dialog({
		title: " ",
		width: 400,
		buttons: [{
			iconCls: "icon-save",
			text: "保存",
			handler: function() {
				let selector = "#image_form";

				checkForm(selector, function () {
					let data = $(selector).serialize();

					ajaxPost(requestUrl, data, function(resp) {
						showMsg(resp.message);

						$(selector).form("clear");
						$("#image_dialog").dialog("close");
						$("#image_list").datagrid("reload");
					}, error);
				});
			}
		}, {
			iconCls: "icon-cancel",
			text: "取消",
			handler: function() {
				resetValue("#upload");

				$("#image_form").form("clear");
				$("#image_dialog").dialog("close");
			}
		}]
	});

	/******************************************************************************************************/

	fetchUploadFilebox("#image_list", "/role_shizhuang_image/upload");

	fetchUploadDialog("/role_shizhuang_image/updateImageById", function () {
		$("#image_list").datagrid("reload");
	});
	
	/******************************************************************************************************/

	/**
	 * 表格体的高度
	 * @type {number}
	 */
	let heigt_body = heigt_tr_with_image * 10;
	/**
	 * 表格高度
	 * @type {number}
	 */
	let height_table = heigt_tool + heigt_header + heigt_body + heigt_footer + heigt_scoll_bar + 2;

	$("#image_list").datagrid({
		url: "/role_shizhuang_image/selectByPage",
		height: height_table,
		fitColumns: true,
		pagination: true,
		toolbar: [{
			iconCls: "icon-add",
			text: "添加",
			handler: function() {
				addHandler();
			}
		}, "-", {
			iconCls: "icon-edit",
			text: "修改",
			handler: function() {
				editHandler();
			}
		}, "-", imageUpdateButton],
		columns: [[
			{field: "id", title: "编号", align: "center"},
			{field: "shizhuangId", title: "时装类型", align: "center", width: 100
				, formatter: function(value, rowData, rowIndex) {
					ajaxGet("/shizhuang_category/selectById", {
						id: value
					}, function(resp) {
						let data = resp.data;

						$("#shizhuang_" + rowIndex).html(data.name);
					}, error);

					return "<div id='shizhuang_" + rowIndex + "'></div>";
				}
			},
			{field: "roleId", title: "角色造型", align: "center", width: 100
				, formatter: function(value, rowData, rowIndex) {
					ajaxGet("/role_category/selectById", {
						id: value
					}, function(resp) {
						let data = resp.data;

						$("#role_" + rowIndex).attr("src", data.image)
							.attr("title", data.name);
					}, error);

					return "<img id='role_" + rowIndex + "' width='" + size + "'/>";
				}
			},
			{field: "image", title: "图片", align: "center", width: 30
				, formatter: function(value) {
					return "<img alt='暂无图片' height='" + size + "' src='" + value + "' />";
				}
			}, deleteColumn
		]]
	});

});